package com.example.cookieshop.common;

import lombok.Data;

import java.io.Serializable;

/**
 * 统一数据格式返回
 */
@Data
public class AjaxResult implements Serializable {

    // 状态码
    private Integer code;

    // 状态码描述信息
    private String message;

    // 返回的数据
    private Object data;

    /**
     * 操作成功返回的数据
     *
     * @return
     */
    public static AjaxResult success(Object data) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode(200);
        ajaxResult.setMessage("");
        ajaxResult.setData(data);
        return ajaxResult;
    }

    public static AjaxResult success(int code, Object data) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode(code);
        ajaxResult.setMessage("");
        ajaxResult.setData(data);
        return ajaxResult;
    }

    /**
     * 操作失败返回的结果
     *
     * @return
     */
    public static AjaxResult fail(int code, String message) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode(code);
        ajaxResult.setMessage(message);
        return ajaxResult;
    }

    public static AjaxResult fail(int code, String message, String data) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setCode(code);
        ajaxResult.setMessage(message);
        ajaxResult.setData(data);
        return ajaxResult;
    }
}
